<?php

class Admin_PartnerController extends Zend_Controller_Action
{

	public function preDispatch(){
		$auth = Zend_Auth::getInstance();
		if(!$auth->hasIdentity()){
			if($this->_request->getActionName() != "login"){
				$this->_redirect("/admin/index/login");
			}
		}
	}
	
    public function init()
    {
        /* Initialize action controller here */
    	$auth = Zend_Auth::getInstance();
    	$infoUser = $auth->getIdentity();
    	$this->view->fullName = $infoUser->USERNAME;
    	$this->_model = new Default_Model_DbTable_Partner();
    	Zend_Layout::startMvc(array("layoutPath"=>APPLICATION_PATH."/layouts/scripts","layout"=>"admin"));
    }

    public function indexAction()
    {
        // action body
        $this->view->headTitle("Ä�á»‘i TÃ¡c","PREPEND");
        $this->view->title = "Quáº£n LÃ½ Ä�á»‘i TÃ¡c";
        $part = $this->_model->getAllPartner();
        $paginator = Zend_Paginator::factory($part);
        $paginator->setItemCountPerPage(9);
        $paginator->setPageRange(5);
        $currentPage = $this->_request->getParam("page",1);
        $paginator->setCurrentPageNumber($currentPage);
        $this->view->allPart = $paginator;
    }
    
    public function addAction(){
    	$this->view->headTitle("ThÃªm Ä�á»‘i TÃ¡c","PREPEND");
    	$this->view->title = "ThÃªm Ä�á»‘i TÃ¡c";
    	$upload = new Private_Upload();
    	if($this->getRequest()->isPost()){
    		if($this->_getParam("PartName")==""){
    			return $this->view->errN = "ChÆ°a Nháº­p TÃªn Ä�á»‘i TÃ¡c";
    		}else{
    			$PName = $this->_request->getParam("PartName");
    		}
    		if($this->_getParam("DateOn")==""){
    			return $this->view->errDOn = "ChÆ°a Nháº­p NgÃ y Báº¯t Ä�áº§u";
    		}else{
    			$DOn = date("Y-m-d",strtotime($this->_request->getParam("DateOn")));
    		}
    		if($this->_getParam("DateOff")==""){
    			return $this->view->errDOff = "ChÆ°a Nháº­p NgÃ y Káº¿t ThÃºc";
    		}else{
    			$DOff = date("Y-m-d",strtotime($this->_request->getParam("DateOff")));
    		}
    		if($this->_getParam("Desc")==""){
    			return $this->view->errDesc = "ChÆ°a Nháº­p MÃ´ Táº£";
    		}else{
    			$Desc = $this->_request->getParam("Desc");
    		}
    		$Stt = $this->_request->getParam("stt");
    		if(!is_numeric($this->_getParam("Position"))){
    			return $this->view->errPos = "Vá»‹ TrÃ­ pháº£i lÃ  1 sá»‘";
    		}else{
    			$Position = $this->_request->getParam("Position");
    		}
    		if($_FILES['Logo']['name']==null && $this->_request->getParam("Link")==""){
    			$Logo = "/ishali/public/uploaded/images/no_image.jpg";
    		}elseif ($_FILES['Logo']["name"] ){
    			$Logo = "/ishali/public/uploaded/images/partner/".$_FILES['Logo']["name"];
    			$upload->upload("partner");
    		}else{
    			$Logo = $this->_request->getParam("Link");
    		}
    		$this->_model->addPartner($PName, $Logo, $DOn, $DOff, $Desc, $Position, $Stt);
    		$this->_redirect("/admin/partner");
    	}
    }
    
    public function editAction(){
    	$this->view->headTitle("Sá»­a Ä�á»‘i TÃ¡c","PREPEND");
    	$this->view->title = "Sá»­a Ä�á»‘i TÃ¡c";
    	$id = $this->_getParam("id");
    	$partner = $this->_model->getPartnerById($id);
    	$this->view->part = $partner;
    	$upload = new Private_Upload();
    	if($this->getRequest()->isPost()){
    		if($this->_getParam("PartName")==""){
    			return $this->view->errN = "ChÆ°a Nháº­p TÃªn Ä�á»‘i TÃ¡c";
    		}else{
    			$PName = $this->_request->getParam("PartName");
    		}
    		if($this->_getParam("DateOn")==""){
    			$this->view->errDOn = "ChÆ°a Nháº­p NgÃ y Báº¯t Ä�áº§u";
    		}else{
    			$DOn = date("Y-m-d",strtotime($this->_request->getParam("DateOn")));
    		}
    		if($this->_getParam("DateOff")==""){
    			$this->view->errDOff = "ChÆ°a Nháº­p NgÃ y Káº¿t ThÃºc";
    		}else{
    			$DOff = date("Y-m-d",strtotime($this->_request->getParam("DateOff")));
    		}
    		if($this->_getParam("Desc")==""){
    			$this->view->errDesc = "ChÆ°a Nháº­p MÃ´ Táº£";
    		}else{
    			$Desc = $this->_request->getParam("Desc");
    		}
    		$Stt = $this->_request->getParam("stt");
    		if(!is_numeric($this->_getParam("Position"))){
    			$this->view->errPos = "Vá»‹ TrÃ­ pháº£i lÃ  1 sá»‘";
    		}else{
    			$Position = $this->_request->getParam("Position");
    		}
    		if($_FILES['Logo']['name']==null && $this->_request->getParam("Link")==""){
    			$Logo = "/ishali/public/uploaded/images/no_image.jpg";
    		}elseif ($_FILES['Logo']["name"]){
    			$Logo = "/ishali/public/uploaded/images/advertisement/".$_FILES['Logo']["name"];
    			$upload->upload("partner");
    		}else{
    			$Logo = $this->_request->getParam("Link");
    		}
    		$this->_model->updatePartner($id,$PName, $Logo, $DOn, $DOff, $Desc, $Position, $Stt);
    		$this->_redirect("/admin/partner");
    	}
    }
    
    public function delAction(){
    	$id = $this->_getParam("id");
    	$this->_model->removePartner($id);
    	$this->_redirect("/admin/partner");
    }
    public function activeAction(){
    	$id = $this->_getParam("id");
    	$this->_model->Active($id);
    	$this->_redirect("/admin/partner");
    }


}

